﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using FluentScheduler;
using ComLib.ComFun;

namespace ScheduleAutoAction
{
    public partial class ucScheduleShutdown : UserControl
    {
        private readonly string AutoShutdownAM = "ShutdownAM";

        private readonly string AutoShutdownPM = "ShutdownPM";

        private bool _isScheduleRuning = false;

        private ScheduleShutdownOption _option = new ScheduleShutdownOption();
        public ScheduleShutdownOption Option
        {
            get { return _option; }
        }

        private void LoadConfig()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }         
            _option.Load();
        }

        public ucScheduleShutdown()
        {
            InitializeComponent();
        }

        public void StartScheduleTask()
        {
            buttonCancel.Text = "停止关机计划";
   
            int hour = _option.ExecuteTimeAtAM.Hour;
            int minute = _option.ExecuteTimeAtAM.Minute;
            JobManager.AddJob(() => IPCCom.ShutDown(), s => s.WithName(AutoShutdownAM).ToRunEvery(1).Days().At(hour, minute));
          
            int hour1 = _option.ExecuteTimeAtPM.Hour;
            int minute1 = _option.ExecuteTimeAtPM.Minute;
            JobManager.AddJob(() => IPCCom.ShutDown(), s => s.WithName(AutoShutdownPM).ToRunEvery(1).Days().At(hour1, minute1));

            _isScheduleRuning = true;
            timer1.Enabled = true;
        }

        public void StopScheduleTask()
        {
            _isScheduleRuning = false;
            timer1.Enabled = false;
            JobManager.RemoveJob(AutoShutdownPM);
            JobManager.RemoveJob(AutoShutdownAM);
            buttonCancel.Text = "启动关机计划";      
            textBoxExcuteTime.Text = "";
            textBoxHour.Text = "";
            textBoxMinute.Text = "";
        }

        public void Init()
        {
            LoadConfig();
            if (_option.Enabled)
            {
                StartScheduleTask();             
            }
            else
            {
                _isScheduleRuning = false;
                timer1.Enabled = false;
                buttonCancel.Text = "启动关机计划";
                textBoxExcuteTime.Text = "";
                textBoxHour.Text = "";
                textBoxMinute.Text = "";
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if (_isScheduleRuning)
            {
                StopScheduleTask();
            }
            else
            {
                StartScheduleTask();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now.Hour < 12)
            {
                TimeSpan ts1 = _option.ExecuteTimeAtAM.TimeOfDay;
                TimeSpan ts2 = DateTime.Now.TimeOfDay;
                TimeSpan ts = ts1.Subtract(ts2).Duration();
                textBoxHour.Text = ts.Hours.ToString();
                textBoxMinute.Text = ts.Minutes.ToString();
                textBoxExcuteTime.Text = _option.ExecuteTimeAtAM.ToString("HH:mm tt");
            }
            if (DateTime.Now.Hour > 12)
            {
                TimeSpan ts1 = _option.ExecuteTimeAtPM.TimeOfDay;
                TimeSpan ts2 = DateTime.Now.TimeOfDay;
                TimeSpan ts = ts1.Subtract(ts2).Duration();
                textBoxHour.Text = ts.Hours.ToString();
                textBoxMinute.Text = ts.Minutes.ToString();
                textBoxExcuteTime.Text = _option.ExecuteTimeAtPM.ToString("HH:mm tt");
            }           
        }
    }
}
